package persistencia.HSQL;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;

import persistencia.DAOS.ArticuloDAO;
import modelo.Articulo;

public class HSQLArticuloDAO implements ArticuloDAO{
	private static Connection connection;
	
	public HashMap<Integer, Articulo> cargarArticulo(Integer in) {
		HashMap<Integer, Articulo> lista = new HashMap<Integer, Articulo>();
		conectar();	
		
		try {
			Statement stm = connection.createStatement();
			ResultSet rs = stm.executeQuery("SELECT a.idArticulo, a.nombre, a.costo, a.empeniable FROM articulo a, lugar l WHERE a.idlugar=l.idlugar AND l.idlugar=" + in);
			
			while(rs.next()){
				lista.put(rs.getInt("idArticulo"), new Articulo(rs.getInt("idArticulo"), rs.getString("nombre"), rs.getInt("costo"), rs.getBoolean("empeniable")));
			}
			
			desconectar();
			stm.close();
		}
		
		catch (SQLException e) {
			e.printStackTrace();
		}
	
		return lista;
	}
	
	public static void conectar(){
		connection = null;
		try {
			Class.forName("org.hsqldb.jdbcDriver");
			connection = DriverManager.getConnection("jdbc:hsqldb:hsql://localhost/", "sa", ""); 	
		}
		
		catch (Exception e) {
			e.printStackTrace();
			System.exit(1);
		}
	}
	
	public static void desconectar(){
		try {
			connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
			System.exit(1);
		}
	}
}
